package title3

import org.apache.spark.{SparkConf, SparkContext}
import title1.IPUtils

object OrderTypeAmount {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[*]").setAppName(this.getClass.getName)
    val sc = new SparkContext(conf)
    //获取订单数据
    val orderData = sc.textFile("data/订单数据.log")
    //获取每条数据的商品种类和成交额
    val typeAndAmount = orderData.map({ t =>
      val line = t.split(" ")
      val orderType = line(2)
      val amount = line(line.length - 1).toInt
      (orderType, amount)
    })
    //按照商品种类聚合并排序
    val result = typeAndAmount.reduceByKey(_+_).coalesce(1).sortBy(-_._2)
    //结果存入mysql
    result.foreachPartition({t=>
      IPUtils.data2Mysql(t,"OrderTypeAmount")
    })
  }
}
